﻿
using Chaunce.Web.Core.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chaunce.Web.Core;
using Chaunce.FrameWork.NetCore.SysCore.Consts;
using Chaunce.FrameWork.NetCore.SysCore.Infrastructure;
using Chaunce.FrameWork.NetCore.SysCore;

namespace Chaunce.Web.Core.Controllers
{
    /// <summary>
    /// api 基类
    /// </summary>
    [AllowAnonymous]
    [Route("api/[controller]")]
    public abstract class BaseApiController : Controller
    {       
        #region api返回结果封装
        /// <summary>
        /// 错误返回
        /// </summary>
        /// <param name="msg">错误消息</param>
        /// <param name="errorCode">错误码</param>
        /// <returns></returns>
        protected virtual JsonResult ToFailResult(string msg, int errorCode=100)
        {
            var result = new ApiResultModel()
            {
                ErrorCode = errorCode,
                Msg = msg
            };
            return Json(result);
        }

        protected virtual JsonResult ToResult(string msg, int errorCode)
        {
            var result = new ApiResultModel()
            {
                ErrorCode = errorCode,
                Msg = msg
            };
            return Json(result);
        }

        /// <summary>
        /// 成功返回结果.body为空
        /// </summary>
        /// <param name="msg">成功提示消息</param>
        /// <returns></returns>
        protected virtual JsonResult ToSuccessResult(string msg = "")
        {
            var result = new ApiResultModel()
            {
                ErrorCode = 0,
                Msg = msg
            };
            return Json(result);
        }

        /// <summary>
        /// 成功返回结果.带body数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="body"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected virtual JsonResult ToSuccessResult<T>(T body,string msg="") 
        {
            var result = new ApiResultModel<T>()
            {
                Body = body,
                ErrorCode = 0,
                Msg = msg
            };
            return Json(result);
        }
        #endregion

        /// <summary>
        /// 获取发起请求的系统平台
        /// </summary>
        /// <returns></returns>
        [NonAction]
        protected  EnumAppPlatform GetPattern()
        {
            try
            {
                var result = Request.Headers["FromApp"].FirstOrDefault();

                var app = result;

                if (string.IsNullOrEmpty(app))
                    return EnumAppPlatform.None;

                var pattern = EnumAppPlatform.None;

                switch (app.ToLower())
                {
                    case "android":
                        pattern = EnumAppPlatform.Android;
                        break;
                    case "ios":
                        pattern = EnumAppPlatform.IOS;
                        break;
                    case "windows":
                        pattern = EnumAppPlatform.Windows;
                        break;
                    case "Web":
                        pattern = EnumAppPlatform.Web;
                        break;
                }
                return pattern;
            }
            catch
            {

            }
            return EnumAppPlatform.None;
        }

        /// <summary>
        /// 获取客户端访问IP
        /// </summary>
        /// <returns></returns>
        [NonAction]
       protected string GetIP()
        {
            return EngineContext.Current.Resolve<IWebHelper>().GetCurrentIpAddress();
        }
    }
}



